package org.syncsoft.overalt.gui.impl.window.addFile;

import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.document.entity.Permission;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractSendFileWindowController;

/**
 * 
 * @author tptfc
 *
 */
public class AddFileWindowController extends AbstractSendFileWindowController<AddFileWindow> {
	public AddFileWindowController(String filePath) {
		super(filePath);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.api.window.AbstractSendFileWindowController#getUpdateRequiredTags(java.lang.String)
	 */
	@Override
	protected Collection<Tag> getUpdateRequiredTags(String filePath) throws SystemException {
		Collection<Tag> requiredTags = getCore().getFolderTags(new File(filePath).getParent());
		return requiredTags;
	}
	
	@Override
	protected Collection<Community> getUpdateRequiredCommunities(String filePath) throws SystemException {
		Integer requiredCommunityCode = getCore().getFolderCommunityId(new File(filePath).getParent());
		
		Collection<Community> communities = new HashSet<Community>();
		if(requiredCommunityCode != null && requiredCommunityCode != -1) {
			Community community = new Community();
			community.setCode(requiredCommunityCode);
			communities.add(community);
		}
		return communities;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doCreateWindow()
	 */
	@Override
	protected AddFileWindow doCreateWindow() throws SystemException {
		AddFileWindow window = new AddFileWindow(this);
		
		Collection<Community> communities = getCore().getComunities();
		window.setCommunities(communities);
		
		return window;
	}
	

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	protected void doSubmit(WindowContextValue context) throws SystemException {
		String title = context.getValue("title");
		String description = context.getValue("description");
		
		String permissionString = context.getValue("permission");
		Permission permission = parsePermission(permissionString);
		
		Collection<Integer> communityCodes = context.getValues("associetedCommunities");
		Set<Community> communities = parseCommunities(communityCodes);
		
		String tagsString = context.getValue("tags");
		Set<Tag> tags = parseTags(tagsString);
		
		getCore().addFile(getFilePath(), title, description, communities, tags, permission);
	}

	public static void main(String[] args) {
		AddFileWindowController controller = new AddFileWindowController("/home/tptfc/Developer/java/syncsoft/amigos-dropbox-fork/a.txt");
		controller.start();
	}
}
